﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI.WebControls;
using consultasSql.consultasVarias;

public partial class Stdby_Busqueda_tabOrdenTrabajo_selectGenerador : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            cargarGrilla("");
            ViewState["ordenarGrilla"] = "ASC";
            Session["filaSeleccionada"] = "-1";
        }
    }

    protected void Grilla_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes["onmouseover"] = "javascript:setMouseOverColor(this);";
            e.Row.Attributes["onmouseout"] = "javascript:setMouseOutColor(this);";
        }
    }


    //cargo grilla
    private void cargarGrilla(string ordenar)
    {
        try
        {
            Grilla.DataSource = darDataSet(ordenar);
            Grilla.DataBind();
        }
        catch { }
    }


    private DataSet darDataSet(string ord)
    {
        string cons = "";

        string Movex = txtBuscaMovex.Text.Trim();

        string Descripcion = txtBuscaDescripcion.Text.Trim();

        string nroCammesa = txtBuscaNroCammesa.Text.Trim();

        if (!String.IsNullOrEmpty(Movex))
        {
            if (Movex.Substring(0, 1) == "*")
            {
                cons += " AND RTRIM(LTRIM(Movex)) LIKE '%' + @Movex";
            }
            else if (Movex.Substring(Movex.Length - 1, 1) == "*")
            {
                cons += " AND RTRIM(LTRIM(Movex)) LIKE @Movex + '%'";
            }
            else
            {
                cons += " AND RTRIM(LTRIM(Movex)) LIKE @Movex";
            }
        }

        if (!String.IsNullOrEmpty(Descripcion))
        {
            if (Descripcion.Substring(0, 1) == "*")
            {
                cons += " AND RTRIM(LTRIM(Descripcion)) LIKE '%' + @Descripcion";
            }
            else if (Descripcion.Substring(Descripcion.Length - 1, 1) == "*")
            {
                cons += " AND RTRIM(LTRIM(Descripcion)) LIKE @Descripcion + '%'";
            }
            else
            {
                cons += " AND RTRIM(LTRIM(Descripcion)) LIKE @Descripcion";
            }
        }

        if (!String.IsNullOrEmpty(nroCammesa))
        {
            if (nroCammesa.Substring(0, 1) == "*")
            {
                cons += " AND RTRIM(LTRIM(Generador.NroCammesa)) LIKE '%' + @NroCammesa";
            }
            else if (nroCammesa.Substring(nroCammesa.Length - 1, 1) == "*")
            {
                cons += " AND RTRIM(LTRIM(Generador.NroCammesa)) LIKE @NroCammesa + '%'";
            }
            else
            {
                cons += " AND RTRIM(LTRIM(Generador.NroCammesa)) LIKE @NroCammesa";
            }
        }

        string ordenar = "";

        if (!String.IsNullOrEmpty(ord.Trim()))
        {
            ordenar = " ORDER BY " + ord.Trim();
        }

        string consulta = "SELECT Generador.Id, Generador.Movex, Generador.Descripcion, Generador.NroCammesa, Generador.ModuloAsig, Generador.ModeloGKQ, Semis.Movex AS 'MovexSemi', Tanque.Movex AS 'MovexTanque', RingMaster.Movex AS 'MovexRingM', Tablero.Movex AS 'MovexTablero', Generador.Importacion, Generador.Ubicacion FROM Generador LEFT JOIN Semis ON Semis.Id = Generador.IdSemi LEFT JOIN Tanque ON Tanque.Id = Generador.IdTanque LEFT JOIN RingMaster ON RingMaster.Id = Generador.IdRingM LEFT JOIN Tablero ON Tablero.Id = Generador.IdTablero WHERE Generador.DeBaja = 0 AND (Generador.IdOtCab IS NULL OR Generador.IdOtCab = @IdOtCab)" + cons + ordenar;


        SqlCommand command = new SqlCommand();
        command.CommandType = CommandType.Text;
        command.CommandText = consulta;

        command.Parameters.Clear();

        command.Parameters.AddWithValue("@IdOtCab", Convert.ToInt32(Session["idOt"].ToString().Trim()));

        if (!String.IsNullOrEmpty(Movex))
        {
            command.Parameters.AddWithValue("@Movex", Movex.Replace("*", ""));
        }

        if (!String.IsNullOrEmpty(Descripcion))
        {
            command.Parameters.AddWithValue("@Descripcion", Descripcion.Replace("*", ""));
        }

        if (!String.IsNullOrEmpty(nroCammesa))
        {
            command.Parameters.AddWithValue("@NroCammesa", nroCammesa.Replace("*", ""));
        }

        return new CDarDataSet().darDataSet(command);
    }


    //cambio de pagina grilla
    protected void grilla_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        try
        {
            Grilla.PageIndex = e.NewPageIndex;
            cargarGrilla("");
        }
        catch { }
    }



    //ordenar grilla
    protected void Grilla_Sorting(object sender, GridViewSortEventArgs e)
    {
        try
        {
            cargarGrilla(e.SortExpression + " " + ordenarGrilla());
        }
        catch { }
    }

    private string ordenarGrilla()
    {
        try
        {
            if (!String.IsNullOrEmpty(ViewState["ordenarGrilla"].ToString()))
            {
                if (ViewState["ordenarGrilla"].ToString() == "DESC")
                {
                    ViewState["ordenarGrilla"] = "ASC";
                }
                else
                {
                    ViewState["ordenarGrilla"] = "DESC";
                }

            }
            else
            {
                ViewState["ordenarGrilla"] = "ASC";
            }
        }
        catch { }

        return ViewState["ordenarGrilla"].ToString();
    }

    //campo seleccionado
    protected void Grilla_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {

            if (!(Convert.ToString(Session["filaSeleccionada"]) == Convert.ToString(Grilla.SelectedIndex)))
            {
                Session["filaSeleccionada"] = Grilla.SelectedIndex.ToString();
                cargarCampos();
            }
            else
            {
                limpiarCampos();
                Grilla.SelectedIndex = -1;
                Session["filaSeleccionada"] = -1;
            }

        }
        catch { }
    }

    private void cargarCampos()
    {
        try
        {
            if (Grilla.SelectedRow != null)
            {
                GridViewRow grilla = Grilla.SelectedRow;

                txtMovex.Text = HttpUtility.HtmlDecode(grilla.Cells[1].Text);
                txtDescripcion.Text = HttpUtility.HtmlDecode(grilla.Cells[2].Text);
                txtSemi.Text = HttpUtility.HtmlDecode(grilla.Cells[7].Text);
                txtTanque.Text = HttpUtility.HtmlDecode(grilla.Cells[8].Text);
                txtRingM.Text = HttpUtility.HtmlDecode(grilla.Cells[9].Text);
                txtTablero.Text = HttpUtility.HtmlDecode(grilla.Cells[10].Text); 
            }
        }
        catch { }
    }

    private void limpiarCampos()
    {
        try
        {
            txtMovex.Text = "";
            txtDescripcion.Text = "";
            txtSemi.Text = "";
            txtTanque.Text = "";
            txtRingM.Text = "";
            txtTablero.Text = ""; 
        }
        catch { }
    }

    protected void btnBuscar_Click(object sender, EventArgs e)
    {
        cargarGrilla("");
    }
}